﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;

namespace Web
{
    public partial class RenRenLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            
            //string postdata = "email=abcddc%40163.com&password=123456&icode=&origURL=http%3A%2F%2Frenren.com%2Findexcon&domain=renren.com&key_id=1&captcha_type=web_login&_rtk=9837a061";
            string postdata = "email=" + txtUserName.Text.Replace("@", "%40") + "&password=" + txtPassword.Text.ToString() + "&icode=&origURL=http%3A%2F%2Frenren.com%2Findexcon&domain=renren.com&key_id=1&captcha_type=web_login&_rtk=9837a061";
            HttpHeader header = new HttpHeader();
            header.Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5";
            header.ContentType = "application/x-www-form-urlencoded";
            header.Method = "POST";
            header.UserAgent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            header.MaxTry = 300;
            string html = GetHtml(txturl.Text.ToString(),GetCookie("http://www.renren.com/",postdata,header),header);
            txtname.Text = html;


        }
        //返回cookie，用于身份认证是否登录
        public static CookieContainer GetCookie(string loginurl,string postdata,HttpHeader header)
        {

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                CookieContainer cc = new CookieContainer();
                request = (HttpWebRequest)WebRequest.Create(loginurl);
                request.Method = header.Method;
                request.ContentType = header.ContentType;
                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabyte.Length;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                //提交请求
                Stream stream = request.GetRequestStream();
                stream.Write(postdatabyte,0,postdatabyte.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                
                CookieCollection cook = response.Cookies;
                //cookie字符串化
                string strcookie = request.CookieContainer.GetCookieHeader(request.RequestUri);


                return cc;


            }
            catch (Exception ex)
            {
                throw ex;
            }

        
        
        }
        public static string GetHtml(string geturl,CookieContainer cookiecontainer,HttpHeader header)
        {
            HttpWebResponse httpwebresponse = null;
            HttpWebRequest httpwebrequest = null;
            try
            {
                httpwebrequest = (HttpWebRequest)WebRequest.Create(geturl);
                httpwebrequest.CookieContainer = cookiecontainer;
                httpwebrequest.ContentType = header.ContentType;
                httpwebrequest.ServicePoint.ConnectionLimit = header.MaxTry;
                httpwebrequest.Referer = geturl;
                httpwebrequest.Accept = header.Accept;
                httpwebrequest.UserAgent = header.UserAgent;
                httpwebrequest.Method = "GET";

                httpwebresponse = (HttpWebResponse)httpwebrequest.GetResponse();
                Stream streamm = httpwebresponse.GetResponseStream();
                StreamReader streamreader = new StreamReader(streamm,Encoding.GetEncoding("utf-8"));
                string html = streamreader.ReadToEnd();
                streamreader.Close();
                httpwebrequest.Abort();
                httpwebresponse.Close();
                return html;              



            }
            catch (Exception e)
            {
                if (httpwebrequest != null) httpwebrequest.Abort();
                if (httpwebresponse != null) httpwebresponse.Close();
                return string.Empty;
            }
        
        
        }
        
    }
}